const TeacherModel = require('../db/models/teacher');

class TeacherService {
    //爬取老师信息
    async addTeacherData (data) {
        const tid = data.tid;

        const result = await TeacherModel.findOne({
            where: { tid }
        });

        if (result) {
            return await TeacherModel.update(data, {
                where: { tid }
            })
        } else {
            return await TeacherModel.create(data);
        }
    }

    //查询老师信息
    async getTeacherData () {
        return await TeacherModel.findAll({
            attributes: {
                exclude: ['tid', 'teacherImg']
              }
        })
    }

    //改变老师上下架状态
    async changeTeacherStatus (id, status) {
        const ret = await TeacherModel.update( { status }, {
            where: { id }
        })

        return ret[0];
    }

    //选择是否为明星老师
    async selectStarTeacher (id, isStar) {
        const ret = await TeacherModel.update( { isStar }, {
            where: { id }
        })

        return ret[0];
    }

    
}

module.exports = new TeacherService();